package cn.yan.luowowo.controller;

import cn.yan.luowowo.domain.Strategy;
import cn.yan.luowowo.domain.StrategyTheme;
import cn.yan.luowowo.query.StrategyQuery;
import cn.yan.luowowo.service.DestinationService;
import cn.yan.luowowo.service.StrategyCatalogService;
import cn.yan.luowowo.service.StrategyService;
import cn.yan.luowowo.service.StrategyThemeService;
import cn.yan.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private StrategyService strategyService;

    @Autowired
    private DestinationService destinationService;

    @Autowired
    private StrategyThemeService strategyThemeService;

    @Autowired
    private StrategyCatalogService strategyCatalogService;


    @RequestMapping("input")
    public String input(Model model,String id){

        //strategy

        //catalogs
        /*
        通常抛出InvocationTargetException是NullPointerException

查看异常具体方法：

将鼠标放到catch 中的Exception ex 上 查看target中抛出的具体异常信息

解决办法：

如果target中异常是找不到某个类 查看sql中返回的类 中字段是否与数据库中的字段一致 不一致 进行修改，修改方法如下例

如果所查找的字段中有被定义为枚举类型的要进行转义
         */
//        model.addAttribute("catalogs",strategyCatalogService.groupList());
        //themes
        model.addAttribute("themes",strategyThemeService.list());


        return "strategy/input";
    }


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQuery qo){

        Page<Strategy> page = strategyService.query(qo);

        model.addAttribute("page",page);

        return "strategy/list";

    }


    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep) throws Exception{
        //查询
       return destinationService.list();

    }

    //修改or添加
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

}
